
export default async function(file_rule, rDir) {
  let db = this.db;

  let raw_tables = await global.metaGlob(file_rule, rDir);
  let db_tables = Object.values(raw_tables).map(v => v.default);
  
  let tbs_names = (await db.listCollections().toArray() || []).map(v => v.name);
  // 表
  for (let _o of db_tables) {
    let _tname = _o.tabname;
    // 存在的表
    if (tbs_names.includes(_tname)) {
      continue; // 跳过
      // TODO 表是否重建
      // await db.dropCollection(_tname);
    }
    // 表
    await db.createCollection(_tname);
    // 索引
    if (_o.indexes?.length > 0) {
      for (let _v of _o.indexes) {
        await db.collection(_tname).createIndex(_v.fds, _v.opts);
      }
    }
    // 默认数据
    if (_o.defs) {
      let _defds = _o.defs instanceof Array ? _o.defs : [_o.defs];
      await db.collection(_tname).insertMany(_defds);
    }
  }

  if (!AUTO_REST_API) { return []; }

  return db_tables.filter(v => v.autoRestApi === true || v.autoRestApi?.at);
};
